package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luhan
 * @create 2022/5/16 18:51
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @RequestMapping("list.html")
    public String listPage(SearchParam searchParam, Model model){
        // 远程调用service-list-client
        // 如果只传递一个参数，那么在远程调用Feign上 可以直接用这个参数去接收
        // 远程调用如果传递多个参数 只用用JSON传输
        Result<Map> result = listFeignClient.list(searchParam);

        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        // searchParam对象
        model.addAttribute("searchParam",searchParam);

        // 品牌面包屑trademarkParam 只有一个
        String trademarkParam  = makeTrademark(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        // 平台属性面包屑propsParamList 多个
        List<Map> propsParamList = makeProps(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        // orderMap
        Map<String, Object> orderMap = dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);

        // trademarkList attrsList goodsList
        // 存储整个对象
        model.addAllAttributes(result.getData());
        return "list/index";
    }

    /**
     * 处理排序
     * @param order
     * @return
     */
    private Map<String, Object> dealOrder(String order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null&&split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        } else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    /**
     * 平台属性面包屑
     * @param searchParam
     * @return
     */
    private List<Map> makeProps(SearchParam searchParam) {
        List<Map> list = new ArrayList<>();
        String[] props = searchParam.getProps();
        if (props!=null&&props.length>0){
            for (String prop : props) {
                // 平台属性:平台属性值
                String[] split = prop.split(":");
                if (split!=null&&split.length==3){
                    Map<String, Object> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 品牌面包屑
     * @param searchParam
     * @return
     */
    private String makeTrademark(SearchParam searchParam) {
        // 品牌：品牌名 只有一个
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            if (split!=null&&split.length==2){
                return "品牌:"+split[1];
            }
        }

        return "";
    }

    /**
     * 获取请求路径中的请求参数
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();

        // 两个入口 关键字检索 分类id检索
        // 判断用户是否通过关键字检索
        // http://list.gmall.com/list.html?keyword=%E6%89%8B%E6%9C%BA
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            sb.append("keyword=");
            sb.append(searchParam.getKeyword());
        }
        // 判断用户是否根据分类id检索
        // http://list.gmall.com/list.html?category1Id=2
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            sb.append("category1Id=");
            sb.append(searchParam.getCategory1Id());
        }
        // http://list.gmall.com/list.html?category2Id=13
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            sb.append("category2Id=");
            sb.append(searchParam.getCategory2Id());
        }
        // http://list.gmall.com/list.html?category3Id=61
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            sb.append("category3Id=");
            sb.append(searchParam.getCategory3Id());
        }

        // 判断用户是否通过品牌检索
        // http://list.gmall.com/list.html?category3Id=61&trademark=4:小米
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            // 进行拼接 要么用过关键字 要么通过分类id
            if (sb.length()>0){
                sb.append("&trademark=");
                sb.append(trademark);
            }
        }

        // 判断用户是否通过平台属性进行检索
        // http://list.gmall.com/list.html?category3Id=61&trademark=4:小米&props=3:6GB:运行内存
        String[] props = searchParam.getProps();
        if (props!=null&&props.length>0){
            if (sb.length()>0){
                // 遍历循环
                for (String prop : props) {
                    sb.append("&props=");
                    sb.append(prop);
                }
            }
        }
        return "list.html?"+sb.toString();
    }
}
